package com.ruoyi.web.controller.homepage;

import java.util.List;
import java.util.ArrayList;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.homepage.domain.TbTag;
import com.ruoyi.homepage.service.ITbTagService;

/**
 * 标签Controller
 * 
 * @author ruoyi
 * @date 2025-07-31
 */
@RestController
@RequestMapping("/homepage/tag")
public class TbTagController extends BaseController
{
    private static final Logger logger = LoggerFactory.getLogger(TbTagController.class);

    @Autowired
    private ITbTagService tbTagService;

    /**
     * 查询标签列表
     */
    @PreAuthorize("@ss.hasPermi('homepage:tag:list')")
    @GetMapping("/list")
    public TableDataInfo list(TbTag tbTag)
    {
        try {
            startPage();
            List<TbTag> list = tbTagService.selectTbTagList(tbTag);
            return getDataTable(list);
        } catch (Exception e) {
            logger.error("查询标签列表失败", e);
            return getDataTable(new ArrayList<>());
        }
    }

    /**
     * 导出标签列表
     */
    @PreAuthorize("@ss.hasPermi('homepage:tag:export')")
    @Log(title = "标签", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TbTag tbTag)
    {
        List<TbTag> list = tbTagService.selectTbTagList(tbTag);
        ExcelUtil<TbTag> util = new ExcelUtil<TbTag>(TbTag.class);
        util.exportExcel(response, list, "标签数据");
    }

    /**
     * 获取标签详细信息
     */
    @PreAuthorize("@ss.hasPermi('homepage:tag:query')")
    @GetMapping(value = "/{tagId}")
    public AjaxResult getInfo(@PathVariable("tagId") Long tagId)
    {
        return success(tbTagService.selectTbTagByTagId(tagId));
    }

    /**
     * 新增标签
     */
    @PreAuthorize("@ss.hasPermi('homepage:tag:add')")
    @Log(title = "标签", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbTag tbTag)
    {
        return toAjax(tbTagService.insertTbTag(tbTag));
    }

    /**
     * 修改标签
     */
    @PreAuthorize("@ss.hasPermi('homepage:tag:edit')")
    @Log(title = "标签", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbTag tbTag)
    {
        return toAjax(tbTagService.updateTbTag(tbTag));
    }

    /**
     * 删除标签
     */
    @PreAuthorize("@ss.hasPermi('homepage:tag:remove')")
    @Log(title = "标签", businessType = BusinessType.DELETE)
	@DeleteMapping("/{tagIds}")
    public AjaxResult remove(@PathVariable Long[] tagIds)
    {
        return toAjax(tbTagService.deleteTbTagByTagIds(tagIds));
    }
}
